/*
 * @Description: 
 * @Author: Crism
 * @Github: https://gitee.com/crism/
 * @Date: 2019-01-25 12:57:54
 * @LastEditors: Crism
 * @LastEditTime: 2019-01-28 17:30:47
 */

const CommomUtil = require('../utils/CommomUtil')
const movie_dao = require('../dao/movie_dao')

module.exports.search_movie = search_movie
async function search_movie(ctx) {
    let { movie_name, movie_count } = CommomUtil.getReqData(ctx)
    let resData = {}
    if (!CommomUtil.isNumber(movie_count) || CommomUtil.isEmpty(movie_name)){
        resData['ret'] = false
        resData['msg'] = '参数不正确!'
        return ctx.body = resData
    }
    let [data,total,page] = await movie_dao.search_movie(movie_name, Math.ceil(movie_count/8))
    resData['movies'] = data
    resData['total'] = total
    resData['page'] = page
    resData['ret'] = data != null ? true : false
    resData['movie_name'] = movie_name
    return ctx.body = resData
}

module.exports.crawl_movie = crawl_movie
async function crawl_movie(ctx) {
    let { movie_id } = CommomUtil.getReqData(ctx)
    let resData = {}
    if (!CommomUtil.isNumber(movie_id)){
        resData['ret'] = false
        resData['msg'] = '参数不正确!'
        return ctx.body = resData
    }
    let movie = await movie_dao.crawl_movie(movie_id)
    resData['movie'] = movie
    resData['ret'] = movie != null ? true : false
    return ctx.body = resData
}

module.exports.crawl_play_url = crawl_play_url
async function crawl_play_url(ctx) {
    let { movie_id } = CommomUtil.getReqData(ctx)
    let resData = {}
    if (!CommomUtil.isNumber(movie_id)){
        resData['ret'] = false
        resData['msg'] = '参数不正确!'
        return ctx.body = resData
    }
    let playlist = await movie_dao.crawl_play_url(movie_id)
    resData['playlist'] = playlist
    resData['ret'] = playlist.length < 1 ? false : true
    return ctx.body = resData
}

module.exports.hot_play_list = hot_play_list
async function hot_play_list(ctx) {
    let { countnum } = CommomUtil.getReqData(ctx)
    let resData = {}
    if (!CommomUtil.isNumber(countnum)){
        resData['ret'] = false
        resData['msg'] = '参数不正确!'
        return ctx.body = resData
    }
    let playlist = await movie_dao.hot_play_list(countnum)
    resData['playlist'] = playlist
    resData['ret'] = playlist.length < 1 ? false : true
    return ctx.body = resData
}